package game.controller;

import game.util.GameLogger;

import javax.swing.*;
import java.awt.*;

/**
 * FlashAnimation.java  Date Created: Nov 2, 2012
 *
 * Purpose: To cause a nation in the chat chooser to flash.
 *
 * Description: When a user receives a messages from another nation, if
 * (s)he are not currently viewing that nations chat window, make that nation
 * in the chat chooser flash a few times.
 *
 * @author Chrisb
 */
public class FlashAnimation extends Thread {
    private JLabel flashingLabel;
    private int timesToFlash;
    private boolean isFlashing;
    private final Color flashRed = new Color(178,34,34); // bright red flash
    private final Color darkRed = new Color(139,0,0); // dark red received

    public FlashAnimation(JLabel label) {
        flashingLabel = label;
        timesToFlash = 4;
        isFlashing = true;
    }

    public FlashAnimation(JLabel label, int timesToFlash) {
        flashingLabel = label;
        this.timesToFlash = timesToFlash;
    }

    public void run() {
        while (timesToFlash > 0) {
            try {
                if (isFlashing)
                    flashingLabel.setForeground(flashRed);
                sleep(500);
                if (isFlashing)
                    flashingLabel.setForeground(darkRed);
                sleep(500);
                timesToFlash--;
            } catch (InterruptedException e) {
                GameLogger.log("FlashAnimation: " + e.getMessage());
                e.printStackTrace();
            }
        }

    }

    public void stopFlashing() {
        isFlashing = false;
    }

    public void allowFlashing() {
        isFlashing = true;
    }
}